﻿using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Lari.Framework.Controllers;
using Lari.Services.Implementation;

namespace Lari.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        private static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.svc/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Core", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //IOC
            var container = GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            log4net.Config.XmlConfigurator.Configure();
            
        }
        private IUnityContainer GetUnityContainer()
        {
            //Create UnityContainer          
            IUnityContainer container = new UnityContainer()
            .RegisterType(typeof(CoreServiceManager), new ContainerControlledLifetimeManager())
            .RegisterType(typeof(UserManagementManager), new ContainerControlledLifetimeManager())
            .RegisterType(typeof(MasterManager), new ContainerControlledLifetimeManager());
            

            //.RegisterType<IControllerActivator, CustomControllerActivator>()
            //.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>()
            //.RegisterType<IMembershipService, AccountMembershipService>()
            //.RegisterInstance<MembershipProvider>(Membership.Provider)
            //.RegisterType<IDatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<IDatabaseFactory>())
            //.RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
            //.RegisterType<ICategoryRepository, CategoryRepository>(new HttpContextLifetimeManager<ICategoryRepository>())
            //.RegisterType<IExpenseRepository, ExpenseRepository>(new HttpContextLifetimeManager<IExpenseRepository>())
            //.RegisterType<ICategoryService, CategoryService>(new HttpContextLifetimeManager<ICategoryService>())
            //.RegisterType<IExpenseService, ExpenseService>(new HttpContextLifetimeManager<IExpenseService>());
            return container;
        }


    }
}